package com.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.util.DBConnection;

/**
 * Servlet implementation class Attention
 */

public class Attention extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Attention() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		
		String schoolNum = request.getParameter("schoolNum1");
		String account   = request.getParameter("account1");
		
		java.sql.Date time = new java.sql.Date((new java.util.Date()).getTime());
		String nowTime = time.toString();
		String sql="select * from user_school where school_num = '"+schoolNum+"' and account = '"+account+"' ";
		String sql1="delete from user_school where school_num = '"+schoolNum+"' and account = '"+account+"' ";
		String sql2="insert into user_school(account,school_num,`time`) values('"+account+"','"+schoolNum+"','"+nowTime+"')";
		DBConnection DB = new DBConnection();
		ResultSet result= null; 
		
		java.io.PrintWriter out = response.getWriter();
		try{
			DB.ConnectDB();
			Connection conn = DB.getConnection();
			Statement sta =conn.createStatement();
			result = sta.executeQuery(sql);
			if(result.next()){//��������Ϊ�Ѿ���ע����ôȡ���ע
				
				//System.out.println(sql1);
				//out.println(sql1);
				sta.executeUpdate(sql1);
			}else{
				
				//System.out.println(sql2);
				//out.println(sql2);
				sta.executeUpdate(sql2);
			}
			
			
			
		}catch(SQLException e){
			e.printStackTrace();
			System.out.println("Attention抛出异常!");
			
		}
		
	}

}
